package cn.pdx.elec.service.impl;



import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.pdx.elec.dao.ElecSystemDDlDao;
import cn.pdx.elec.dao.ElecUserDao;
import cn.pdx.elec.domain.ElecUser;
import cn.pdx.elec.service.ElecUserService;
import cn.pdx.elec.util.MD5keyBean;
import cn.pdx.elec.util.StringHelper;
import cn.pdx.elec.web.form.ElecUserForm;


/**
 *  @Repository 、@Service 和 @Controller 
           在目前的 Spring 版本中，这 3 个注释和 @Component 是等效的，但是从注释类的命名上，3 个注释分别和持久层 、业务层 和控制层 相对应。
 * 
 *@Transactional(readOnly=true),定义类方法的事务为只读,不能写入数据库
 * 业务类方法只有增、删、改为可写,其他事务都为只读
 * @author 彭定旭
 */
@Transactional(readOnly=true)
@Service(ElecUserService.SERVICE_NAME)   //spring不用配置
public class ElecUserServiceImpl implements ElecUserService {
   
	//业务层调用dao层;用注解
	@Resource(name=ElecUserDao.SERVICE_NAME)
	private ElecUserDao elecUserDao;
	
	/**
	 * 引入数据字典查询
	 */
	@Resource(name=ElecSystemDDlDao.SERVICE_NAME)
	private ElecSystemDDlDao elecSystemDDlDao;
  
	
	
	/**
	 * @Name:findElecUserList
	 * @Description:查询用户列表信息，判断用户姓名是否为空,如果不为空按照用户姓名组织查询条件
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-3(创建日期)
	 * @parameters:ElecUserForm elecUserForm  VO对象
	 * @return:List<ElecUserForm> 用户对象结果集对象
	 * 因为都是查询,所以一个方法共用就可以了,只要判断条件就是了
	 */
	public List<ElecUserForm> findElecUserList(ElecUserForm elecUserForm) {
		
		//组织查询条件
		String hqlWhere="";
		
		List<String> paramsList=new ArrayList<String>();
		
		if(elecUserForm!=null && elecUserForm.getUserName()!=null && !elecUserForm.getUserName().equals(""))
		{
			hqlWhere+="and o.userName like ?";
			
			paramsList.add("%"+ elecUserForm.getUserName() +"%");
		}
		
		//集合对象变数组
		Object[] params=paramsList.toArray();
		
		//组织排序
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		
		orderby.put("o.onDutyDate", "desc");
		
		List<ElecUser> list=elecUserDao.findCollectionByConditionNoPage(hqlWhere, params, orderby);
		
		List<ElecUserForm> formList=this.elecUserPOListToVOList(list);
		
		return formList;
	}


	/**
	 * @Name:elecUserPOListToVOList
	 * @Description:查询用户列表信息，PO->VO
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-3(创建日期)
	 * @parameters:List<ElecUser> list
	 * @return:List<ElecUserForm> 用户对象结果集对象
	 * 因为都是查询,所以一个方法共用就可以了,只要判断是否带条件的查询就是了
	 */
	
	private List<ElecUserForm> elecUserPOListToVOList(List<ElecUser> list) {
		
		List<ElecUserForm> formList=new ArrayList<ElecUserForm>();
		
		ElecUserForm elecUserForm=null;
		
		for(int i=0;list!=null&& i<list.size();i++)
		{
			ElecUser elecUser=list.get(i);
			elecUserForm=new ElecUserForm();
			elecUserForm.setUserID(elecUser.getUserID());
			elecUserForm.setLogonName(elecUser.getLogonName());
			elecUserForm.setUserName(elecUser.getUserName());
			
			/**
			 * 查询数据字典,判断值
			 */
			//数据库查询数字字典
		   elecUserForm.setSexID(elecUser.getUserID()!=null && !elecUser.getUserID().equals("")? elecSystemDDlDao.findDDlName(elecUser.getSexID(),"性别"):"");
		   elecUserForm.setContactTel(elecUser.getContactTel());
			//数据库查询数字字典
		   elecUserForm.setIsDuty(elecUser.getIsDuty()!=null && !elecUser.getIsDuty().equals("")?elecSystemDDlDao.findDDlName(elecUser.getIsDuty(),"是否在职"):"");
		   formList.add(elecUserForm);
		}
		
		return formList;
	}


	/**
	 * @Name:saveElecUser
	 * @Description:保存用户
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-3(创建日期)
	 * @parameters:ElecUserForm elecUserForm
	 * @return:无
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void saveElecUser(ElecUserForm elecUserForm) {
		
		//VO对象->PO
		ElecUser elecUser=this.elecUserVOToPO(elecUserForm);
		
		/**
		 * 这个是判断那个新增用户保存;和修改用户的保存,因为编辑用户的时候隐藏一个ID,所以用ID判断是否是新增一个用户还是编辑过后修改用户才保存的
		 * 方法可以共用,少了个！号，擦,差点搞死我
		 */
		if(elecUserForm.getUserID()!=null && !elecUserForm.getUserID().equals(""))
		{
			//如果是修改用户的话就更新
			elecUserDao.update(elecUser);
			
		}else{
			
			//新增的话就保存
			elecUserDao.save(elecUser);
			
		}
	}


	/**
	 * @Name:elecUserVOToPO
	 * @Description:将用户信息从VO对象转换成PO对象
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-3(创建日期)
	 * @parameters:ElecUserForm elecUserForm VO对象
	 * @return:PO对象
	 */
	private ElecUser elecUserVOToPO(ElecUserForm elecUserForm) {
		
		ElecUser elecUser=new ElecUser();
		
		if(elecUserForm!=null)
		{
			if(elecUserForm.getUserID()!=null && !elecUserForm.getUserID().equals(""))
			{
				//System.out.println(elecUserForm.getUserID());
				elecUser.setUserID(elecUserForm.getUserID());
				
				if(elecUserForm.getOffDutyDate()!=null &&!elecUserForm.getOffDutyDate().equals(""))
				{
				elecUser.setOffDutyDate(StringHelper.stringConvertDate(elecUserForm.getOffDutyDate()));
				}
			}
			elecUser.setJctID(elecUserForm.getJctID());
			elecUser.setUserName(elecUserForm.getUserName());
			elecUser.setLogonName(elecUserForm.getLogonName());
			
			//2013-4-20,使用MD5密码加密
			String password=elecUserForm.getLogonPwd();
			String md5password="";
			//初始密码,密码为六个0
			if(password==null || "".equals(password))
			{
				password="000000";
			}
			String md5flag=elecUserForm.getMd5flag();
			//修改时如果两次密码一致,不需要进行密码加密
			if(md5flag!=null && md5flag.equals("1"))
			{
				md5password=password;
			}
			//无论是新增,还是处理修改(修改了当前密码),都需要进行密码加密
			else{
				
				 MD5keyBean md5=new MD5keyBean();
				 md5password=md5.getkeyBeanofStr(password);
			}
			
			elecUser.setLogonPwd(md5password);
			
			elecUser.setSexID(elecUserForm.getSexID());
			if(elecUserForm.getBirthday()!=null &&!elecUserForm.getBirthday().equals(""))
			{
				elecUser.setBirthday(StringHelper.stringConvertDate(elecUserForm.getBirthday()));
			}
			elecUser.setAddress(elecUserForm.getAddress());
			elecUser.setContactTel(elecUserForm.getContactTel());
			elecUser.setEmail(elecUserForm.getEmail());
			elecUser.setMobile(elecUserForm.getMobile());
			elecUser.setIsDuty(elecUserForm.getIsDuty());
			if(elecUserForm.getOnDutyDate()!=null && !elecUserForm.getOnDutyDate().equals(""))
			{
				elecUser.setOnDutyDate(StringHelper.stringConvertDate(elecUserForm.getOnDutyDate()));
			}
			elecUser.setRemark(elecUserForm.getRemark());
		}
		return elecUser;
	}


	/**
	 * @Name:findElecUser
	 * @Description:获取用户详细信息,使用用户ID查询
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-5(创建日期)
	 * @parameters:ElecUserForm elecUserForm VO对象存放用户id
	 * @return:用户详细信息
	 */
	public ElecUserForm findElecUser(ElecUserForm elecUserForm) {
		
		String userID=elecUserForm.getUserID();
		
		//System.out.println(userID);
		ElecUser elecUser=elecUserDao.findObjectById(userID);
		
		//PO->VO,还是用原来那个模型对象,因为编辑页面也是VO对象来的,所以传过来的还是原来那个对象就可以了
	     elecUserForm=this.elecUserPOToVO(elecUser,elecUserForm);
	     
		return elecUserForm;
	}

	/**
	 * @Name:elecUserPOToVO
	 * @Description:获取用户详细信息,PO->VO
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-5(创建日期)
	 * @parameters:ElecUser elecUser,ElecUserForm elecUserForm
	 * @return:VO
	 * 始终保持模型驱动里的那个VO对象,因为返回的也是VO对象来的
	 */
	private ElecUserForm elecUserPOToVO(ElecUser elecUser,ElecUserForm elecUserForm) {
		
		//不用新建一个实例，始终保持模型驱动里的那个VO对象,因为返回的也是VO对象来的,所以传过来的还是原来那个对象就可以了
		//ElecUserForm elecUserForm=new ElecUserForm();
		if(elecUser!=null)
		{  
			elecUserForm.setUserID(elecUser.getUserID());
			elecUserForm.setJctID(elecUser.getJctID());
			elecUserForm.setUserName(elecUser.getUserName());
			elecUserForm.setLogonName(elecUser.getLogonName());
			elecUserForm.setLogonPwd(elecUser.getLogonPwd());
			elecUserForm.setSexID(elecUser.getSexID());
			elecUserForm.setBirthday(String.valueOf((elecUser.getBirthday()!=null && !elecUser.getBirthday().equals("")?elecUser.getBirthday():"" )));
			elecUserForm.setAddress(elecUser.getAddress());
			elecUserForm.setContactTel(elecUser.getContactTel());
			elecUserForm.setEmail(elecUser.getEmail());
			elecUserForm.setMobile(elecUser.getMobile());
			elecUserForm.setIsDuty(elecUser.getIsDuty());
		    elecUserForm.setOnDutyDate(String.valueOf((elecUser.getOnDutyDate()!=null && !elecUser.getOnDutyDate().equals("")?elecUser.getOnDutyDate():"")));
		    elecUserForm.setOffDutyDate(String.valueOf(elecUser.getOffDutyDate()!=null && !elecUser.getOffDutyDate().equals("")?elecUser.getOffDutyDate():""));
			elecUserForm.setRemark(elecUser.getRemark());
		}
		return elecUserForm;
	}


	/**
	 * @Name:deleteElecUser
	 * @Description:根据Id删除对象
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-6(创建日期)
	 * @parameters:ElecUserForm elecUserForm
	 * @return:无
	 */
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void deleteElecUser(ElecUserForm elecUserForm) {

		
		String userID=elecUserForm.getUserID();
		
		elecUserDao.deleteObjectByIds(userID);
	}


	/**
	 * @Name:checkLogonName
	 * @Description:使用登录名作为查询条件查询数据库,判断当前登录名是否在数据库存在
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-5(创建日期)
	 * @parameters:String logonName 当前登录名
	 * @return:checkflag:判断当前登录名在数据库中是否存在的标识
		       checkflag=1：如果值为1,说明当前登录名在数据库有重复记录,则不能进行保存
		       checkflag=2：如果值为2,说明当前登录名在数据库没有重复记录,可以进行保存
	 */
	public String checkLogonName(String logonName) {
		
		String hqlWhere=" and o.logonName=?";
		
		Object[]params={logonName};
		
		List<ElecUser> list=elecUserDao.findCollectionByConditionNoPage(hqlWhere, params, null);
		
		String checkflag="";
		
		if(list!=null && list.size()>0)
		{
			
			checkflag="1";
			
		}else{
			
			checkflag="2";
			
		}
		return checkflag;
	}


	/**
	 * @Name:findElecUserByLogonName
	 * @Description:使用登录名获取用户的详细信息,用于登录
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-21(创建日期)
	 * @parameters:String name  登录名
	 * @return:ElecUser 存放用户信息
	 */
	public ElecUser findElecUserByLogonName(String name) {
		
		String hqlWhere=" and o.logonName= ?";
		
		Object[]params={name};
		
		List<ElecUser> list=elecUserDao.findCollectionByConditionNoPage(hqlWhere, params, null);
		
		ElecUser elecUser=null;
		
		if(list!=null && list.size()>0)
		{
			elecUser=list.get(0);
		}
		return elecUser;
	}


	/**
	 * @Name:findElecPopedomByLogonName
	 * @Description:使用登录名获取当前用户的权限
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-22(创建日期)
	 * @parameters:String name  登录名
	 * @return:String 存放用户权限
	 */
	public String findElecPopedomByLogonName(String name) {
		
		List<Object> list=elecUserDao.findElecPopedomByLogonName(name);
		StringBuffer buffer=new StringBuffer("");
		for(int i=0;list!=null && i<list.size();i++)
		{
			Object object=list.get(i);
			buffer.append(object.toString());
		}
		return buffer.toString();
	}


	/**
	 * @Name:findeElecRoleByLogonName
	 * @Description:使用登录名获取当前用户的角色
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-22(创建日期)
	 * @parameters:String name  登录名
	 * @return:Hashtable<String, String> 存放用户角色
	 */
	public Hashtable<String, String> findeElecRoleByLogonName(String name) {
		
		List<Object []> list=elecUserDao.findeElecRoleByLogonName(name);
		
		Hashtable<String, String> hashtable=null;
		
		if(list!=null && list.size()>0)
		{
			hashtable=new Hashtable<String, String>();
			
			for(int i=0;i<list.size();i++)
			{
				Object[] object=list.get(i);
				hashtable.put(object[0].toString(), object[1].toString());
			}
		}
		return hashtable;
	}
	
	
	

	
}
